package com.ftg.learn.chapter12.core;

import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.Arrays;

/**
 * 代理模式
 *
 * @author 布衣
 */
@Component
@Aspect
@EnableAspectJAutoProxy
public class SpringAopProxy {

    long startTime;
    //写通知  切点之间的通知
    @Before("execution(public * com.ftg.learn.chapter12.*.SpringAop.abc*())")
    public void test() {
        startTime = System.currentTimeMillis();
        System.out.println("开始");
    }

    @SneakyThrows
    @Around("execution(public void com.ftg.learn.chapter12.core.SpringAop.test1(..))")
    public Object test5(ProceedingJoinPoint pjp) {
    //百度  一下
        System.out.println("目标类的名称" + pjp.getTarget().getClass().getName());
        System.out.println("目标类的参数" + Arrays.toString(pjp.getArgs()));
//        目标类里面的切点 pjp
        System.out.println("around start");
        // 调用方法 proceed();目标类里面的切点方法执行
        Object y = pjp.proceed();
        System.out.println("around end");
        return y;
    }

    @After("execution(public void com.ftg.learn.chapter12.core.SpringAop.test1(..))")
    public void test2() {
        System.out.println("结束 ");
        System.out.println("耗时 " + (System.currentTimeMillis() - startTime));
    }

    @AfterReturning("execution(public void com.ftg.learn.chapter12.core.SpringAop.test1(..))")
    public void test3() {
        System.out.println("AfterReturning结束 ");
        System.out.println("耗时 " + (System.currentTimeMillis() - startTime));
    }

    @AfterThrowing("execution(public void com.ftg.learn.chapter12.core.SpringAop.test1(..))")
    public void test4() {
        System.out.println("AfterThrowing ");
        System.out.println("耗时 " + (System.currentTimeMillis() - startTime));
    }

}
